package strings;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.regex.Pattern;

import file.csv;

public class deserializer
{
	public String data = "";

	/** reads control_element properteis **/
	public String data_format = ""; // csv

	public String first_line_contains_keys = ""; // true or false
	public String value_enclosed_by = ""; // " e.g.

	public String value_pair_delimiter = ""; // separates each key=value pair
	public String element_delimiter = ""; // separates each object-element (containing multiple key=value pairs)
	public String key_value_delimiter = ""; // separates key from value in this case "=" key=value

	public String line_delimiter = ""; // " e.g.
	public String values_delimiter = ""; // , e.g.
	public String elements_per_line = ""; // ; e.g.
	// String data = (String) parameters.get("data"); //
	
	/** alternative constructor with many parameters usually come witha http-request **/
	public deserializer(HashMap parameters)
	{
		// TODO Auto-generated constructor stub
		this.data = (String) parameters.get("data");
		
		/** reads control_element properteis **/
		this.data_format = (String) parameters.get("data_format"); // csv
		
		this.first_line_contains_keys = (String) parameters.get("first_line_contains_keys"); // true or false
		this.value_enclosed_by = (String) parameters.get("value_enclosed_by"); // " e.g.
		
		this.value_pair_delimiter = (String) parameters.get("value_pair_delimiter"); // separates each key=value pair
		this.element_delimiter = (String) parameters.get("element_delimiter"); // separates each object-element (containing multiple key=value pairs)
		this.key_value_delimiter = (String) parameters.get("key_value_delimiter"); // separates key from value in this case "=" key=value
		
		this.line_delimiter = (String) parameters.get("line_delimiter"); // " e.g.
		this.values_delimiter = (String) parameters.get("values_delimiter"); // , e.g.
		this.elements_per_line = (String) parameters.get("elements_per_line"); // ; e.g.
		// String data = (String) parameters.get("data"); //
	}

	/** alternative constructor with manual parameters **/
	public deserializer(String element_delimiter, String value_pair_delimiter, String key_value_delimiter)
	{
		this.value_pair_delimiter = value_pair_delimiter; // separates each key=value pair
		this.element_delimiter = element_delimiter; // separates each object-element (containing multiple key=value pairs)
		this.key_value_delimiter = key_value_delimiter; // separates key from value in this case "=" key=value
		// String data = (String) parameters.get("data"); //
	}

	/** converts serialized data to an HashMap **/
	public HashMap deserialize(String data)
	{
		HashMap RESULT = new HashMap();
		
		Pattern element_delimiter_pattern = Pattern.compile(element_delimiter);
		Pattern value_pair_delimiter_pattern = Pattern.compile(value_pair_delimiter);
		Pattern key_value_delimiter_pattern = Pattern.compile(key_value_delimiter);
		
		// split lines by line_delimiter
		// split by line
	    String[] OBJECTS = element_delimiter_pattern.split(data);
	    HashMap NEW_OBJECT = new HashMap();
	    String[] KEYS = null;
	    String[] VALUES = null;
	    String KEY = "";
	    String[] KEYVALUES = null;
	    String[] KEY_VALUES = null;
	    String[] PROPERTIES = null;
	    String VALUE = "";
	    // ArrayList<String> KEYS = new ArrayList<String>();

	    // separate elements
		for(Integer counter=0; counter<OBJECTS.length; counter++)
	    {
			VALUES = value_pair_delimiter_pattern.split(OBJECTS[counter]);

			// separate key=value-pairs/properties
			for (Integer counter1=0; counter1<VALUES.length; counter1++)
			{
				VALUE = "";
				KEY = "";
				KEYVALUES = value_pair_delimiter_pattern.split(VALUES[counter1]);
				KEY_VALUES = key_value_delimiter_pattern.split(KEYVALUES[0]);
				
				// collect data
				try
				{
					KEY = KEY_VALUES[0]; // key
				}
				catch (Exception e)
				{
				}
				try
				{
					VALUE = KEY_VALUES[1]; // value
				}
				catch (Exception e)
				{
				}

				NEW_OBJECT.put(KEY, VALUE);
				// System.out.println(KEY+"="+VALUE); // works perfectly
			}
			RESULT.put(counter, NEW_OBJECT);
	    }
		
		return RESULT;
	}
	
	public HashMap deserialize(HashMap parameters)
	{
    	HashMap LINES = null;

    	if(data_format.equals("csv"))
    	{
    		csv CSV = new csv();
    		LINES = CSV.csv2HashMap(data, elements_per_line, data_format, first_line_contains_keys, value_enclosed_by, values_delimiter, line_delimiter);
    	}

    	if(data_format.equals("array"))
    	{
    		LINES = deserialize(data);
    	}

    	return LINES;
	}
}